package org.jeecg.modules.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.TenantModel;
import org.jeecg.common.util.DateUtils;
import org.jeecg.config.mybatis.TenantContext;
import org.jeecg.modules.chg.service.IChgArrearsService;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 欠费违约金计算
 */

@Slf4j
public class FeePenaltyJob implements Job {

	@Autowired
	private IChgArrearsService arrearsService;
	@Autowired
	private IQuartzJobService quartzJobService;
	@Autowired
	private ISysBaseAPI baseAPI;
	/**
	 * 若参数变量名修改 QuartzJobController中也需对应修改
	 */
	private String parameter;

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		log.info(String.format(" Boot 欠费违约金计算 FeePenaltyJob !  开始时间:" + DateUtils.getTimestamp()));

		//需要变量多租户
		List<TenantModel> tenantList = baseAPI.queryTenant();
		if(null != tenantList && tenantList.size()>0) {
			//遍历租户
			for (TenantModel tenantModel : tenantList) {
				TenantContext.setTenant(tenantModel.getId() + "");

				QuartzJob quartzJob = quartzJobService.getById(this.parameter);
				if (null != quartzJob) {
					arrearsService.expensesPenalty();
				}
			}
		}
		log.info(String.format(" Boot 欠费违约金计算 FeePenaltyJob !  结束时间:" + DateUtils.getTimestamp()));
	}

}
